﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VehicleScale.Models;

namespace VehicleScale.EntityMappers
{
    public class CustomerMapper
    {
        #region Methods With Relation
        public static CustomerModel ToModel(Customer entity)
        {
            if (entity == null) return null;
            CustomerModel model = new CustomerModel()
            {
                CustomerID = entity.CustomerID,
                CustomerName = entity.CustomerName,
                Telephone = entity.Telephone,
                Identification = entity.Identification,
                Address = entity.Address,
                FaxNumber = entity.FaxNumber,
                TaxCode = entity.TaxCode,
                CreatedByID = entity.CreatedByID,
                CreationDate = entity.CreationDate,
                ModifiedByID = entity.ModifiedByID,
                ModifiedDate = entity.ModifiedDate,
                Active = entity.Active
                //VehicleScales = new List<VehicleScaleModel>()
            };

            //if (entity.VehicleScales != null)
            //{
            //    foreach (var item in entity.VehicleScales)
            //    {
            //        model.VehicleScales.Add(VehicleScaleMapper.ToModel(item, 0));
            //    }
            //}

            return model;
        }

        public static Customer ToEntity(Customer entity, CustomerModel model)
        {
            if (model == null) return null;

            entity.CustomerID = model.CustomerID;
            entity.CustomerName = model.CustomerName;
            entity.Telephone = model.Telephone;
            entity.Identification = model.Identification;
            entity.Address = model.Address;
            entity.FaxNumber = model.FaxNumber;
            entity.TaxCode = model.TaxCode;
            entity.CreatedByID = model.CreatedByID;
            entity.CreationDate = model.CreationDate;
            entity.ModifiedByID = model.ModifiedByID;
            entity.ModifiedDate = model.ModifiedDate;
            entity.Active = model.Active;
            //entity.VehicleScales = new System.Data.Linq.EntitySet<VehicleScale>();

            //if (model.VehicleScales != null)
            //{
            //    foreach (var item in model.VehicleScales)
            //    {
            //        entity.VehicleScales.Add(VehicleScaleMapper.ToEntity(new VehicleScale(), item));
            //    }
            //}

            return entity;
        }
        #endregion

        #region Methods Without Relation
        public static CustomerModel ToModel(Customer entity, int childLevel)
        {
            if (entity == null) return null;
            CustomerModel model = new CustomerModel()
            {
                CustomerID = entity.CustomerID,
                CustomerName = entity.CustomerName,
                Telephone = entity.Telephone,
                Identification = entity.Identification,
                Address = entity.Address,
                FaxNumber = entity.FaxNumber,
                TaxCode = entity.TaxCode,
                CreatedByID = entity.CreatedByID,
                CreationDate = entity.CreationDate,
                ModifiedByID = entity.ModifiedByID,
                ModifiedDate = entity.ModifiedDate,
                Active = entity.Active,

            };

            if (childLevel > 0)
            {
                model.VehicleScales = new List<VehicleScaleModel>();
                foreach (var item in entity.VehicleScales)
                {
                    model.VehicleScales.Add(VehicleScaleMapper.ToModel(item, childLevel - 1));
                }
            }

            return model;

        }

        public static Customer ToEntity(Customer entity, CustomerModel model, int childLevel)
        {
            if (model == null) return null;

            entity.CustomerID = model.CustomerID;
            entity.CustomerName = model.CustomerName;
            entity.Telephone = model.Telephone;
            entity.Identification = model.Identification;
            entity.Address = model.Address;
            entity.FaxNumber = model.FaxNumber;
            entity.TaxCode = model.TaxCode;
            entity.CreatedByID = model.CreatedByID;
            entity.CreationDate = model.CreationDate;
            entity.ModifiedByID = model.ModifiedByID;
            entity.ModifiedDate = model.ModifiedDate;
            entity.Active = model.Active;

            if (childLevel > 0)
            {
                entity.VehicleScales = new System.Data.Linq.EntitySet<VehicleScale>();
                foreach (var item in model.VehicleScales)
                {
                    entity.VehicleScales.Add(VehicleScaleMapper.ToEntity(new VehicleScale(), item));
                }
            }

            return entity;
        }
        #endregion
    }
}
